package com.agriculture.monitor.controller;


import com.agriculture.monitor.base.ResultInfo;
import com.agriculture.monitor.dto.BasePage;
import com.agriculture.monitor.dto.PasswordRequest;
import com.agriculture.monitor.dto.UserPage;
import com.agriculture.monitor.entity.User;
import com.agriculture.monitor.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-07-15
 */
@Log4j2
@RestController
@RequestMapping("/web/user")
public class UserController {

    private AtomicInteger atomicInteger = new AtomicInteger();

    @Autowired
    IUserService userService;

    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public ResultInfo<?> login(@RequestBody User user) {
        return userService.login(user);
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @PostMapping("/create")
    public ResultInfo<?> create(@RequestBody User user) {
        return userService.create(user);
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping("/change")
    public ResultInfo<?> change(@RequestBody User user) {
        return userService.change(user);
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping("/delete")
    public ResultInfo<?> delete(@RequestBody User user) {
        return ResultInfo.OK(userService.removeById(user.getId()));
    }

    @ApiOperation(value = "禁用", notes = "禁用")
    @PostMapping("/disable")
    public ResultInfo<?> disable(@RequestBody User user) {
        return ResultInfo.OK(userService.update(new LambdaUpdateWrapper<User>().eq(User::getId, user.getId()).set(User::getStatus, 0)));
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/change/password")
    public ResultInfo<?> password(@RequestBody @Validated PasswordRequest passwordRequest) {
        return userService.password(passwordRequest);
    }

    @ApiOperation(value = "下级查询", notes = "下级查询")
    @PostMapping("/children/page")
    public ResultInfo<?> childrenPage(@RequestBody BasePage basePage) {
        return userService.childrenPage(basePage);
    }

    @ApiOperation(value = "用户查询", notes = "用户查询")
    @PostMapping("/list")
    public ResultInfo<?> list(@RequestBody UserPage basePage) {
        return ResultInfo.OK(userService.listPage(basePage));
    }

    @ApiOperation(value = "用户查询", notes = "用户查询")
    @PostMapping("/one")
    public ResultInfo<User> User(@RequestBody User user) {
        return ResultInfo.OK(userService.getById(user.getId()));
    }
}
